package com.yimiyisu.contract_cust.controller;

import com.zen.ZenController;
import com.zen.ZenData;
import com.zen.ZenEngine;
import com.zen.ZenResult;
import com.zen.annotation.AccessRole;
import com.zen.annotation.Inject;
import com.zen.domain.ZenUser;
import com.zen.enums.ZenAction;
import com.zen.enums.ZenRole;
import com.zen.kit.JsonKit;
import com.zen.enums.ZenRole;
import com.zen.kit.JsonKit;
import com.zen.kit.StringKit;
import com.zen.kit.UserKit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@AccessRole(ZenRole.SIGNATURE)
public class Member extends ZenController {
    @Inject
    private ZenEngine zenEngine;

    public ZenResult getCompany(ZenData data){
        ZenResult zenResult = zenEngine.execute("list/manager", data);
        if (zenResult.isEmpty()) return ZenResult.fail("暂无公司");
        List<Map> list = zenResult.asList(Map.class);
        List<String> companyIds = new ArrayList<>();
        list.forEach(map -> {
            companyIds.add(map.get("companyId").toString());});
        return ZenResult.success().setData(companyIds);
    }

    /**
     * 用户信息补全
     */
    public ZenResult updateUser(ZenData data) {
        ZenUser user = new ZenUser();
        user.setId(data.getUid());
        user.setNick(data.get("nick"));
        user.setEmail(data.get("email"));
        UserKit.update(user);
        UserKit.updateMobile(data.getUid(), data.get("mobile"));
        return ZenResult.success();
    }

    public ZenResult info(ZenData data){
        ZenUser zenUser = UserKit.get(data.getUid());
        if (data.isEmpty()){
            ZenResult zenResult = zenEngine.execute("get/manager", data);
            Map<String,String> info = new HashMap<>();
            info.put("title",zenUser.getNick());
            info.put("id",zenResult.get("id"));
            if (StringKit.isNotEmpty(zenResult.get("phone"))) info.put("phone",zenResult.get("phone"));
            else info.put("phone",zenUser.getMobile());
            if (StringKit.isNotEmpty(zenUser.getAvatar())) info.put("avatar",zenUser.getAvatar());
            info.put("email",zenUser.getEmail());
            info.put("companyId",zenResult.get("companyId"));
            info.put("uid",zenResult.get("uid"));
            return ZenResult.success().setData(info);
        }
        zenEngine.execute("patch/manager", data);
        zenUser.setAvatar(data.get("avatar"));
        zenUser.setEmail(data.get("email"));
        zenUser.setNick(data.get("title"));
        zenUser.setMobile(data.get("phone"));
        UserKit.update(zenUser);
        return ZenResult.success("保存成功");
    }


}
